package demoxsgl_300.com.jj0588;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

import org.greenrobot.eventbus.EventBus;

import demoxsgl_300.com.jj0588.bean.MyEvent;
import demoxsgl_300.com.jj0588.utils.CheckPermissionsUtil;
import demoxsgl_300.com.jj0588.vie.CameraSurfaceView;


public class Record_SD_Activity extends AppCompatActivity {
    private CameraSurfaceView cameraSurfaceView;
    SensorManager sm;
    Sensor ligthSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
//        getSupportActionBar().hide()
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        ligthSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
        sm.registerListener(new MySensorListener(), ligthSensor, SensorManager.SENSOR_DELAY_NORMAL);
        CheckPermissionsUtil checkPermissionsUtil = new CheckPermissionsUtil(this);
        checkPermissionsUtil.requestAllPermission(this);

        cameraSurfaceView = (CameraSurfaceView) findViewById(R.id.cameraSurfaceView);
        findViewById(R.id.capture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraSurfaceView.capture();
            }
        });
        ((ToggleButton) findViewById(R.id.record)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cameraSurfaceView.startRecord();
                    iskai = true;
                    //设置录制时长为10秒视频
//                    cameraSurfaceView.startRecord(10000, new MediaRecorder.OnInfoListener() {
//                        @Override
//                        public void onInfo(MediaRecorder mr, int what, int extra) {
//                            cameraSurfaceView.stopRecord();
//                            buttonView.setChecked(false);
//                        }
//                    });
                } else {
                    cameraSurfaceView.stopRecord();
                    iskai = false;
                    cameraSurfaceView.switchLight(false);
                    MyEvent tianQi = new MyEvent();
                    tianQi.setSc("finish");
                    EventBus.getDefault().post(tianQi);
                }

            }
        });
        ((ToggleButton) findViewById(R.id.light)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });
        ((ToggleButton) findViewById(R.id.runBack)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                cameraSurfaceView.setRunBack(isChecked);
            }
        });
        ((ToggleButton) findViewById(R.id.switchCamera)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                cameraSurfaceView.setDefaultCamera(!isChecked);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraSurfaceView.closeCamera();
    }

    boolean iskai = false;

    public class MySensorListener implements SensorEventListener {

        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }


        //获取光线的强度
        public void onSensorChanged(SensorEvent event) {

            float lux = event.values[0];//获取光线强度


            int retval = Float.compare(lux, (float) 20.0);


            if (iskai) {
                Log.e("888", "xx" + retval);
//                Toast.makeText(Record_SD_Activity.this, "" + retval, Toast.LENGTH_SHORT).show();
                if (retval == -1) {//光线强度>10.0
                    cameraSurfaceView.switchLight(true);


//                CameraManager.get().closeLight();//关闪光灯
                } else {
                    cameraSurfaceView.switchLight(false);

//                   mediaUtils.kaiqi();
//                CameraManager.get().openLight();//开闪光灯
                }
            }

        }
    }

}
